{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import options as opts\n",
    "from pyecharts.globals import SymbolType\n",
    "from pyecharts.charts import Bar,Pie,Page,WordCloud\n",
    "from pyecharts.globals import ThemeType,SymbolType\n",
    "import numpy\n",
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv('comments_1.csv',names=[\"id\",\"nickName\",\"userLevel\",\"cityName\",\"score\",\"startTime\"])\n",
    "attr = [\"一星\", \"二星\", \"三星\", \"四星\", \"五星\"]\n",
    "score = df.groupby(\"score\").size()  # 分组求和\n",
    "value = [\n",
    "    score.iloc[0] + score.iloc[1]+score.iloc[1],\n",
    "    score.iloc[3] + score.iloc[4],\n",
    "    score.iloc[5] + score.iloc[6],\n",
    "    score.iloc[7] + score.iloc[8],\n",
    "    score.iloc[9] + score.iloc[10],\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\文件\\\\jupyter文件\\\\数据分析\\\\哪吒.html'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 饼图分析\n",
    "# 暂时处理，不能直接调用value中的数据\n",
    "attr = [\"一星\", \"二星\", \"三星\", \"四星\", \"五星\"]\n",
    "value = [286, 43, 175, 764, 10101]\n",
    "\n",
    "pie = (\n",
    "    Pie(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))\n",
    "    .add('',[list(z) for z in zip(attr, value)])\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title='哪吒等级分析'))\n",
    "    .set_series_opts(label_opts=opts.LabelOpts(formatter=\"{b}:{c}\"))\n",
    ")\n",
    "# pie.render_notebook()\n",
    "pie.render(\"哪吒.html\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Loading model from cache C:\\Users\\admin\\AppData\\Local\\Temp\\jieba.cache\n",
      "Loading model cost 1.154 seconds.\n",
      "Prefix dict has been built succesfully.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<wordcloud.wordcloud.WordCloud at 0x1bf751864a8>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import jieba\n",
    "import matplotlib.pyplot as plt   #生成图形\n",
    "from  wordcloud import WordCloud,STOPWORDS,ImageColorGenerator\n",
    "\n",
    "df = pd.read_csv(\"comments_1.csv\",names =[\"id\",\"nickName\",\"userLevel\",\"cityName\",\"content\",\"score\",\"startTime\"])\n",
    "\n",
    "comments = df[\"content\"].tolist()\n",
    "# comments\n",
    "df\n",
    "\n",
    "# 设置分词\n",
    "comment_after_split = jieba.cut(str(comments), cut_all=False)  # 非全模式分词，cut_all=false\n",
    "words = \" \".join(comment_after_split)  # 以空格进行拼接\n",
    "\n",
    "stopwords = STOPWORDS.copy()\n",
    "stopwords.update({\"电影\",\"最后\",\"就是\",\"不过\",\"这个\",\"一个\",\"感觉\",\"这部\",\"虽然\",\"不是\",\"真的\",\"觉得\",\"还是\",\"但是\"})\n",
    "\n",
    "bg_image = plt.imread('bg.jpg')\n",
    "#生成\n",
    "wc=WordCloud(\n",
    "    width=1024,\n",
    "    height=768,\n",
    "    background_color=\"white\",\n",
    "    max_words=200,\n",
    "    mask=bg_image,            #设置图片的背景\n",
    "    stopwords=stopwords,\n",
    "    max_font_size=200,\n",
    "    random_state=50,\n",
    "    font_path='C:/Windows/Fonts/simkai.ttf'   #中文处理，用系统自带的字体\n",
    "    ).generate(words)\n",
    "\n",
    "#产生背景图片，基于彩色图像的颜色生成器\n",
    "image_colors=ImageColorGenerator(bg_image)\n",
    "#开始画图\n",
    "plt.imshow(wc.recolor(color_func=image_colors))\n",
    "#为背景图去掉坐标轴\n",
    "plt.axis(\"off\")\n",
    "#保存云图\n",
    "plt.show()\n",
    "wc.to_file(\"评价.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
